install.packages("Seurat")
library(Seurat)
library(dplyr)
library(ggplot2)
setwd("D://文件//1")
pbmc.data<-Read10X(data.dir = "pbmc3k_filtered_gene_bc_matrices")
pbmc <- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k",min.cells = 3, min.features = 200)
pbmc[['percent.mt']] <- PercentageFeatureSet(pbmc, pattern = "^MT-|^mt-")
VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"),  ncol = 3)
pbmc <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)
pbmc <- NormalizeData(pbmc, normalization.method = "LogNormalize", scale.factor = 10000)
all.genes <- rownames(pbmc)
pbmc <- ScaleData(pbmc, features = all.genes)
pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)
pbmc <- ScaleData(pbmc)
VariableFeaturePlot(pbmc)
top10 <- head(VariableFeatures(pbmc), 10)
pbmc <- RunPCA(pbmc, features = VariableFeatures(object = pbmc))
pbmc <- FindNeighbors(pbmc, dims = 1:10)
pbmc <- FindClusters(pbmc, resolution = 0.5)
pbmc <- RunUMAP(pbmc, dims = 1:10)
UMAPPlot(pbmc)
pbmc.markers <- FindAllMarkers(pbmc, only.pos = TRUE, min.pct = 0.25, logfc.threshold = 0.25)
pbmc.markers %>%
  group_by(cluster) %>%
  slice_max(n = 2, order_by = avg_log2FC)
FeaturePlot(pbmc,features = c("RPL5","RPL10","RPL19","RPL36"))
VlnPlot(pbmc, features = c("RPL5","RPL10","RPL19","RPL36"))
##top2基因
top2<-pbmc.markers %>%
  group_by(cluster) %>%
  slice_max(n = 2, order_by = avg_log2FC)
##气泡图
p10<-DotPlot(object=pbmc,assay ='RNA',features = top2$gene)+theme(axis.text.x =  element_text(angle = 45,hjust = 1) )
print(p10)